/***************************************************************************
*   Copyright (C) 2006 by Yuri Ovcharenko                                 *
*   amwsoft@gmail.com                                                     *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include "pluginmanager.h"
#include "dcplugin.h"

#include <QtCore>

PluginManager::PluginManager( QObject *parent )
    : QObject( parent )
{
  dirList.clear();
}

PluginManager::~PluginManager()
{
}

QStringList PluginManager::dirs()
{
  return QStringList(dirList);
}

void PluginManager::searchPlugins(const QStringList & dirs)
{
  if (dirs.isEmpty())
    return;

  QStringList newDirs;
  for (int i = 0; i < dirs.size(); i++)
    if ((! dirList.contains(dirs[i])) && (! newDirs.contains(dirs[i])))
      newDirs.append(dirs[i]);

  for (int i = 0; i < newDirs.size(); i++)
  {
    QDir pdir(newDirs[i]);
    pdir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
    QStringList flist(pdir.entryList());
    for (int f = 0; f < flist.size(); f++)
    {
      QString libName = QString("%1/%2").arg(pdir.path()).arg(flist[f]);
      if (!QLibrary::isLibrary(libName))
        continue;
      QPluginLoader loader(libName);
      if (!loader.load())
        continue;
      DCPlugin *p = qobject_cast<DCPlugin *>(loader.instance());
      if (!p)
      {
        loader.unload();
        continue;
      }
      bool found = false;
      for (int j = 0; j < plugins.size(); j++)
      {
        if (plugins[j]->name() == p->name())
        {
          found = true;
          loader.unload();
          break;
        }
      }
      if (!found)
      {
        plugins.append(p);
      }
    }
  }

  dirList += newDirs;
}

int PluginManager::count()
{
  return plugins.size();
}

DCPlugin *PluginManager::plugin(const QString & name) const
{
	for (int i = 0; i < plugins.count(); i++) {
		if (plugins.at(i)->name() == name)
			return plugins.at(i);
	}
	return 0;
}

DCPlugin *PluginManager::plugin(int index) const
{
	if (index < 0 || index >= plugins.count())
		return 0;
	return plugins.at(index);
}

QString PluginManager::pluginName(int index) const
{
  if ((index < 0) || (index >= plugins.size()))
    return QString();

  return QString(plugins[index]->name());
}

QWidget *PluginManager::pluginWidget(int index) const
{
  if ((index < 0) || (index >= plugins.size()))
    return 0;

  return plugins[index]->widget();
}

void PluginManager::deactivatePlugin(int index)
{
  if ((index < 0) || (index >= plugins.size()))
    return;

  plugins[index]->deleteWidget();
}

void PluginManager::savePluginSettings(int index)
{
  if ((index < 0) || (index >= plugins.size()))
    return;

  plugins[index]->saveSettings(PluginSettingsDir);
}

void PluginManager::restorePluginSettings(int index)
{
  if ((index < 0) || (index >= plugins.size()))
    return;

  plugins[index]->restoreSettings(PluginSettingsDir);
}

void PluginManager::setSettingsDir(const QString & dir)
{
  PluginSettingsDir = dir;
}

void PluginManager::slotPluginChanged()
{
}


/* End of file */
